import json

import requests

from common.config import cmslogin, getconf


class MetaeventManage:
    def getEventCreatePeopleSelect(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventCreatePeopleSelect"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={}
        r=requests.post(url=url,data=json.dumps(data),headers=header)
        #print('创建人列表',r.json())
        return r.json()

    def getEventPageList(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventPageList"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"appId":"test2","appVersion":"2.2.2.1","creator":1272,"pageName":"home","current":1,"size":50,"sortFields":"gmtCreate","sortType":"desc"}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('元事件列表',r.json())
        return r.json()

    def getEventAppSelect(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventAppSelect"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"appKeyWord":"test"}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('选择应用',r.json())
        return r.json()

    def getEventAppVerSelect(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventAppVerSelect"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"appKeyWord":"test"}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('选择应用版本',r.json())
        return r.json()

    def getEventPageNameSelect(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventPageNameSelect"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"pageName":"home","appId":"test2","appVersion":"2.2.2.1"}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('选择页面名称',r.json())
        return r.json()

    def getEventDetail(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventDetail/75"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        r=requests.get(url,headers=header)
        #print('详情',r.json())
        return r.json()

    def getUsableAppNames(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getUsableAppNames"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        r=requests.get(url,headers=header)
        #print('编辑-获取可用的app',r.json())
        return r.json()

    def getEventTypeList(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/com/getEventTypeList"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"eventType":0}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('编辑-获取事件类型列表',r.json())
        return r.json()

    def getHardwareList(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/com/getHardwareList"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"hardwareCode":""}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('编辑-获取硬件',r.json())
        return r.json()



    def editEvent(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/editEvent"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"type":0,"appName":"test2","appId":"test2","appVersion":"2.2.2.1","eventName":"test","eventType":0,"eventCode":"PAGEVIEW","pageName":"home","elementName":"bu2","eventUrl":"http://packet.rongyi.com/ry/easydata/resources/2005081034197088/2005081034190387.png","hardwareCode":"","hardwareName":"","businessDesc":"","paramFieldList":[{"id":113,"eventId":75,"baseFieldId":19,"paramKey":"we","paramValue":"we","paramRemark":"we","paramType":0},{"id":114,"eventId":75,"baseFieldId":19,"paramKey":"we","paramValue":"1:we","paramRemark":"we","paramType":1}],"id":75}
        r=requests.post(url,data=json.dumps(data),headers=header)
        print('编辑-保存',r.json())
        return r.json()

    def saveCopyEvent(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/saveCopyEvent"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"type":1,"appName":"test2","appId":"test2","appVersion":"2.2.2.1","eventName":"autotest","eventType":0,"eventCode":"CLICK","pageName":"pageindex","elementName":"button","eventUrl":"http://packet.rongyi.com/ry/easydata/resources/2009291507491907/2009291507491867.png","hardwareCode":"","hardwareName":"","businessDesc":"","paramFieldList":[{"id":620,"eventId":175,"baseFieldId":19,"paramKey":"type","paramValue":"start","paramRemark":"end","paramType":0},{"id":621,"eventId":175,"baseFieldId":30,"paramKey":"weee","paramValue":"1:er,2:po","paramRemark":"cvb","paramType":1}]}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('复制并新建',r.json())
        return r.json()

    def saveEvent(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/saveEvent"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data = {"type": 0, "appName": "test2", "appId": "test2", "appVersion": "2.2.2.1", "eventName": "autotest",
            "eventType": 0, "eventCode": "CLICK", "pageName": "pageindex", "elementName": "button",
            "eventUrl": "http://packet.rongyi.com/ry/easydata/resources/2009291507491907/2009291507491867.png",
            "hardwareCode": "", "hardwareName": "", "businessDesc": "", "paramFieldList": [
            {"baseFieldId": 19, "paramType": 0, "paramKey": "type", "paramRemark": "end", "paramValue": "start",
             "editable": True, "paramKeyState": "success", "paramValueState": "success", "paramRemarkState": "success"},
            {"baseFieldId": 30, "paramType": 1, "paramKey": "weee", "paramRemark": "cvb", "paramValue": "1:er,2:po",
             "editable": True, "paramKeyState": "success", "paramValueState": "success",
             "paramRemarkState": "success"}], "id": None}
        r=requests.post(url,data=json.dumps(data),headers=header)
        return r.json()

    def getUsableAppVer(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getUsableAppVer"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={'appId': 'test2','appName': 'test2'}
        r=requests.get(url=url,params=data,headers=header)
        #print('可用版本号',r.json())
        return r.json()

    def getEventAppFieldList(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "easydata-web/v1/event/getEventAppFieldList"
        token = cmslogin(env)
        header={"Content-Type": "application/json;charset=UTF-8","token":token}
        data={"appId":"test2","appName":"test2","appVersion":"2.2.2.1"}
        r=requests.post(url,data=json.dumps(data),headers=header)
        #print('属性清单列表',r.json())
        return r.json()




#b=MetaeventManage()
#b.getEventAppFieldList('test')

